package pers.youqu.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.youqu.common.exception.YqException;
import pers.youqu.item.dto.SpecParamDTO;
import pers.youqu.item.mapper.SpecParamMapper;
import pers.youqu.item.po.SpecParam;
import pers.youqu.item.service.SpecParamService;

import java.util.List;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/6 21:27
 * @Description TODO
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> findSpecParam(Long cId, Long gId, Boolean searching) {
        // 健壮性判断
//        if(cId == null && gId == null){
//            // 条件不能都为空
//            throw new YqException(400, "查询条件不能为空！");
//        }
        List<SpecParam> specParamList = query().eq(cId != null, "category_id", cId)
                .eq(gId != null, "group_id", gId)
                .eq(searching != null, "searching", searching).list();
        return SpecParamDTO.convertEntityList(specParamList);
    }

    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        boolean flag = save(specParamDTO.toEntity(SpecParam.class));
        if (!flag) {
            throw new YqException(500, "新增规格参数错误");
        }
    }

    @Override
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        boolean flag = updateById(specParamDTO.toEntity(SpecParam.class));
        if (!flag) {
            throw new YqException(500, "修改规格参数错误");
        }
    }
}
